package com.vicki.music;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.vicki.music.MediaUtils.ServiceToken;

public class MediaPlayDetail extends Activity implements OnClickListener {

	private static String TAG_Media = "MediaPlayDetail";
	private Button btn_play;
	private Button btn_next;
	private Button btn_prev;
	private Button btn_mode;
	private Button btn_lyrce;
	private ServiceToken mToken;
	private MediaUpdateReceiver scanSdReceiver;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play);

		initView();

		registerBroadcastRecevier();

		mToken = MediaUtils.bindToService(this, serviceConnection);

	}

	private void registerBroadcastRecevier() {
		IntentFilter intentfilter = new IntentFilter(
				Intent.ACTION_MEDIA_SCANNER_STARTED);
		intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
		intentfilter.addDataScheme("file");
		scanSdReceiver = new MediaUpdateReceiver();
		registerReceiver(scanSdReceiver, intentfilter);
	}

	private void initView() {
		btn_play = (Button) findViewById(R.id.btn_start);
		btn_next = (Button) findViewById(R.id.btn_next);
		btn_prev = (Button) findViewById(R.id.btn_prev);
		btn_mode = (Button) findViewById(R.id.btn_mode);
		btn_lyrce = (Button) findViewById(R.id.btn_lyrc);
		btn_play.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		btn_prev.setOnClickListener(this);
		btn_lyrce.setOnClickListener(this);
		btn_play.setOnClickListener(this);
		btn_mode.setOnClickListener(this);

	}

	@Override
	protected void onDestroy() {
		if (mToken != null) {
			MediaUtils.unbindFromService(mToken);
		}
		unregisterReceiver(scanSdReceiver);
		super.onDestroy();
	}

	ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {

		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_start:
			play();
			break;
		case R.id.btn_lyrc:
			setShuffleMode();
			break;
		case R.id.btn_prev:
			prev();
			break;
		case R.id.btn_next:
			next();
			break;
		case R.id.btn_mode:
			setRepeatMode();
			break;
		default:
			break;
		}
	}

	private void setRepeatMode() {
		MediaUtils utils = MediaUtils.getInstense();
		utils.setRepeatMode();
		switch (utils.getRepeatMode()) {
		case MediaPlaybackService.REPEAT_ALL:
			Toast.makeText(this, "repeat_mode is repeat_all",
					Toast.LENGTH_SHORT).show();
			break;
		case MediaPlaybackService.REPEAT_CURRENT:
			Toast.makeText(this, "repeat_mode is repeat_current",
					Toast.LENGTH_SHORT).show();
			break;
		case MediaPlaybackService.REPEAT_OFF:
			Toast.makeText(this, "repeat_mode is repeat_off",
					Toast.LENGTH_SHORT).show();
			break;
		}
	}

	private void setShuffleMode() {
		MediaUtils utils = MediaUtils.getInstense();
		utils.setShuffleMode();
		if (utils.getShuffleMode() == MediaPlaybackService.SHUFFLE_ON) {
			Toast.makeText(this, "shuffle_mode is on", Toast.LENGTH_SHORT)
					.show();
		} else {
			Toast.makeText(this, "shuffle_mode is off", Toast.LENGTH_SHORT)
					.show();
		}

	}

	private void next() {
		Log.i(TAG_Media, "click next");
		MediaUtils utils = MediaUtils.getInstense();
		utils.next();
	}

	private void prev() {
		Log.i(TAG_Media, "click prev");
		MediaUtils utils = MediaUtils.getInstense();
		utils.prev();
	}

	private void play() {
		Log.i(TAG_Media, "click play");
		MediaUtils utils = MediaUtils.getInstense();
		utils.play();
	}

}
